<?php
namespace app\teacher\controller;

use app\teacher\controller\PublicController;
use app\common\model\TemplateFooterModel;
use app\common\logic\admin\TemplateFooter;

//尾部模板
class TemplateFooterController extends PublicController
{

	public function __construct(){
		parent::__construct();
		$this->check();
		$this->ids = $this->install();
	}

	//列表
    public function index(){
        $data = TemplateFooterModel::where('user_id','in',$this->ids)->order('sort asc,footer_id desc')->paginate(20);
        $this->assign('data',$data);
        $this->assign('page',$data->render());
        return $this->fetch();
    }
    //添加
    public function add(){
        return $this->fetch();
    }
    //提交添加
    public function toAdd(TemplateFooter $TemplateFooter){
        $param = $this->request->param();
        if (in_array('', $param['post']['content'])) {
            $this->error('字段不能为空');die;
        }
        $param['post']['user_id'] = $this->user_id;
        $rs = $TemplateFooter->save($param);

        if ($rs) {
            $this->success('保存成功','index');
        }else{
            $this->error('保存失败');
        }
    }
    //编辑
    public function edit(){
        $param = $this->request->param();
        $data = TemplateFooterModel::get($param['id']);
        $data['content'] = json_decode($data['content'],true);
        $this->assign('data',$data);
        return $this->fetch();
    }
    //提交编辑
    public function toEdit(TemplateFooter $TemplateFooter){
        $param = $this->request->param();
        if (in_array('', $param['post']['content'])) {
            $this->error('字段不能为空');die;
        }
        $rs = $TemplateFooter->save($param,2);

        if ($rs) {
            $this->success('保存成功','index');
        }else{
            $this->error('保存失败');
        }
    }
    //设置
    public function set(){
        $param = $this->request->param();
        $rs = TemplateFooterModel::where('footer_id',$param['footer_id'])->update(['status'=>$param['status']]);
        if ($rs) {
            $this->success('修改成功');
        }else{
            $this->error('修改失败');
        }
    }
    //批量设置
    public function sets(){
        $param = $this->request->param();
        $ids = implode($param['ids'],',');
        $rs = TemplateFooterModel::where('footer_id','in',$ids)->update(['status'=>$param['status']]);
        if ($rs) {
            $this->success('修改成功');
        }else{
            $this->error('修改失败');
        }
    }
    //预览
    public function look(){
        $param = $this->request->param();
        $data = TemplateFooterModel::get($param['id']);
        $data['content'] = json_decode($data['content'],true);
        $this->assign('data',$data);
        return $this->fetch();
    }
    //排序
    public function sort(){
        $param = $this->request->param();
        foreach($param['sort'] as $key => $value){
            TemplateFooterModel::where('footer_id',$key)->update(['sort'=>$value]);
        }
        $this->success('操作成功!');
    }

	
}
